package com.woniu.service.impl;


import com.woniu.dao.*;
import com.woniu.pojo.*;
import com.woniu.service.StudentService;

import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;
    @Resource
    private InterviewDao interviewDao;
    @Resource
     MarkDao markDao;
    @Resource
    private StageDao stageDao;
    @Resource
    private JobDao jobDao;
    @Resource
    private ClazzDao clazzDao;
    /**
     * 增加学生
     * @param student
     */
    @Override
    public void add(Student student) {
        studentDao.insertSelective(student);
    }

    /**
     * 删除学生
     * @param sid
     */
    @Override
    public void delStudent(Integer sid) {
         studentDao.deleteByPrimaryKey(sid);
    }

    /**
     * 修改学生信息
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        studentDao.updateByPrimaryKeySelective(student);
    }

    /**
     * 查询一个学生
     * @param sid
     * @return
     */
    @Override
    public Student selectStudent(Integer sid) {
        return studentDao.selectByPrimaryKey(sid);
    }

    /**
     * 查询学生成绩
     * @param mid
     * @return
     */
    @Override
    public Mark selectMark(Integer mid) {
       return markDao.selectByPrimaryKey(mid);
    }

    /**
     * 修改学生成绩
     * @param mark
     */
    @Override
    public void updateMark(Mark mark) {
        markDao.updateByPrimaryKeySelective(mark);
    }

    /**
     * 查询所有阶段
     * @return
     */
    @Override
    public List<Stage> findAllStage() {
        List<Stage> stageList = stageDao.selectByExample(null);
        return stageList;
    }

    /**
     * 查询所有成绩
     * @return
     */
    @Override
    public List<Mark> findAllMarks() {
        List<Mark> markList = markDao.selectByExample(null);
        return markList;
    }

    /**
     * 通过cid查找为就业的学生
     * @param cid
     * @return
     */
    @Override
    public List<Student> getStudentsByCid(Integer cid) {
        //先查找处所有已经毕业登记的学生id
        List<Job> jobs = jobDao.selectByExample(null);
        List<Integer> sidList = new ArrayList<>();
        for(int i = 0;i<jobs.size();i++) {
            sidList.add(jobs.get(i).getSid());
        }
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        if(cid>0){
            criteria.andCidEqualTo(cid);
        }
        if(sidList.size()>0){
            criteria.andSidNotIn(sidList);
        }
        return studentDao.selectByExample(studentExample);
    }

    @Override
    public void delStudentByCid(Integer cid) {
        StudentExample studentExample=new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andCidEqualTo(cid);
        studentDao.deleteByExample(studentExample);
    }

    @Override
    public List<Clazz> selectStudentClazz() {
       List<Student> studentList= studentDao.selectByExample(null);
        Set<Integer> cidSet=new HashSet<>();
        for (Student student : studentList) {
           cidSet.add(student.getCid()) ;
        }
        List<Integer> cidList=new ArrayList<>();
        for (Integer cid : cidSet) {
            cidList.add(cid);
        }
        List<Clazz> clazzList=new ArrayList<>();
        for (Integer cid : cidList) {
            clazzList.add(clazzDao.selectByPrimaryKey(cid));
        }

        return clazzList;
    }

    /**
     * 查询学生
     * @return
     */
    @Override
    public List<Student> selectByExample() {
        List<Student> studentList = studentDao.selectByExample(null);
        for (Student student : studentList) {
            int sid=student.getSid();
            InterviewExample interviewExample=new InterviewExample();
            interviewExample.createCriteria().andSidEqualTo(sid);
            List<Interview> interviewList = interviewDao.selectByExample(interviewExample);
            if (interviewList.size()!=0){
                student.setIfDoInterview("已做");
            }else{
                student.setIfDoInterview("未做");
            }
        }
        return studentList;
    }

    /**
     * 学生列表查询加分页
     * @param pageBean
     * @param student
     * @return
     */
    @Override
    public List<Student> getStudentsByPage(PageBean pageBean, Student student) {
        //封装PageBean信息
        StudentExample studentExample=new StudentExample();
        StudentExample.Criteria criteria=studentExample.createCriteria();
        //来源信息
        if(student.getResource()!=null&&!student.getResource().equals("")) {
            criteria.andResourceLike("%" + student.getResource() + "%");
        }
        //学生姓名
        if(student.getStname()!=null&&!student.getStname().equals("")) {
            criteria.andStnameLike("%" + student.getStname() + "%");
        }
        if(student.getCid()!=null&&!student.getCid().equals("")) {
            criteria.andCidEqualTo(student.getCid());
        }
        if(student.getSid()!=null&&!student.getSid().equals("")) {
            criteria.andSidEqualTo( student.getSid());
        }
        List<Student> studentList=new ArrayList<>();
        Set<Integer> sidDoSet=new HashSet<>();

        List<Student> studentListIfDo=new ArrayList<>();

       //得到所有学生，将所有学生id和面试表里学生id进行对比，判断学生是否经过面试,面试表里如果有学生id，将学生id提取出来
        studentList=studentDao.selectByExample(null);
        for (Student s : studentList) {
            Integer sid=s.getSid();
            InterviewExample interviewExample=new InterviewExample();

            interviewExample.createCriteria().andSidEqualTo(sid);
            List<Interview> interviewList = interviewDao.selectByExample(interviewExample);
            if (interviewList.size()!=0){
                for (Interview interview : interviewList) {
                    sidDoSet.add(interview.getSid());
                }
            }
        }
        //将学生id转换为List集合,sidDoList所存储数据为面试表里已经有的学生id
        List<Integer> sidDoList=new ArrayList<>();
        for (Integer integer : sidDoSet) {
            sidDoList.add(integer);
        }

            if (student.getIfDoInterview()!=null&&!student.getIfDoInterview().equals("")){
                if (student.getIfDoInterview().equals("已做")){criteria.andSidIn(sidDoList);
                }
                if (student.getIfDoInterview().equals("未做")){criteria.andSidNotIn(sidDoList);}
            }


        //判断搜索条件，已做面试和未做面试
        //将搜索条件和分页进行结合

        Long countRow=studentDao.countByExample(studentExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage=pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);
        //分页查询
        studentExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        studentExample.setLimit(pageBean.getPageSize());
        //得到搜索条件的学生集合，若没有搜索条件则是分页查询所有学生
        studentListIfDo=studentDao.selectByExample(studentExample);

        //对学生类里添加的新属性IfDoInterview进行赋值，面试了的学生属性为已做，没有的学生，属性为未做

        if(studentListIfDo.size()!=0){
        for (Student s : studentListIfDo) {
            int sid=s.getSid();
            InterviewExample interviewExample=new InterviewExample();
            interviewExample.createCriteria().andSidEqualTo(sid);
            List<Interview> interviewList = interviewDao.selectByExample(interviewExample);
            if (interviewList.size()!=0){
                s.setIfDoInterview("已做");
            }else{
                s.setIfDoInterview("未做");
            }
        }
        }
        return studentListIfDo;
    }

    /**
     * 增加成绩
     * @param mark
     */
    @Override
    public Boolean insertMark(Mark mark) {
        //循环打印出所有学生id和阶段id
        List<Mark> markList = markDao.selectByExample(null);
        for (Mark m:markList){
            if(mark.getSid().equals(m.getSid())&& mark.getSno().equals(m.getSno())){
                   mark.setMid(m.getMid());
                   markDao.updateByPrimaryKeySelective(mark);
                   return true;
            }
        }
        markDao.insertSelective(mark);
        return true;
    }

    /**
     * 查询学生周考成绩列表
     * @param mark
     * @param student
     * @return
     */
    @Override
    public List<Mark> getStudents(Mark mark,Student student) {
        //封装查询条件
        StudentExample studentExample=new StudentExample();
        StudentExample.Criteria criteria=studentExample.createCriteria();
        //来源信息
        if(student.getResource()!=null&&!student.getResource().equals("")) {
            criteria.andResourceLike("%" + student.getResource() + "%");
        }
        //学生姓名
        if(student.getStname()!=null&&!student.getStname().equals("")) {
            criteria.andStnameLike("%" + student.getStname() + "%");
        }
        //学生班级
        if(student.getCid()!=null&&!student.getCid().equals("")) {
            criteria.andCidEqualTo( student.getCid());
        }
        List<Integer> sidList=new ArrayList<>();
        List<Student> studentList=studentDao.selectByExample(studentExample);
        for (Student s:studentList) {
             sidList.add(s.getSid());
        }
        //成绩列表
        MarkExample markExample=new MarkExample();
        MarkExample.Criteria markExampleCriteria = markExample.createCriteria();
        markExampleCriteria.andSidIn(sidList);
        List<Mark> markList=markDao.selectByExample(markExample);
        for (Mark m3:markList) {
            if(m3.getSno()==1){
                m3.setFirstStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }else if(m3.getSno()==2){
                m3.setSecondStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }else if(m3.getSno()==3){
                m3.setThirdStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }else if(m3.getSno()==4){
                m3.setProcessStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }
        }
        System.out.println("marklist:"+markList);
        System.out.println("sidlist:"+sidList);

        List<Mark> markList2=new ArrayList<>();
    //列表去重
    boolean flag = false;
    for (Mark old : markList) {
        for (Mark news : markList2) {
            if (news.getSid() == old.getSid()) {
                if(old.getSno()==1){
                    news.setFirstStage(old.getFirstStage());
                }
                if(old.getSno()==2){
                    news.setSecondStage(old.getSecondStage());
                }
                if(old.getSno()==3){
                    news.setThirdStage(old.getThirdStage());
                }
                if(old.getSno()==4){
                    news.setProcessStage(old.getProcessStage());
                }
                flag = true;
                break;
            } else {
                flag = false;
            }
        }
        if (!flag) {
            markList2.add(old);
        }
    }
    System.out.println(markList2);

        return markList2;
    }

    /**
     * 查询学生阶段考成绩列表
     * @param mark
     * @param student
     * @return
     */
    @Override
    public List<Mark> getStudentsProcess(Mark mark,Student student) {
        StudentExample studentExample=new StudentExample();
        StudentExample.Criteria criteria=studentExample.createCriteria();
        //来源信息
        if(student.getResource()!=null&&!student.getResource().equals("")) {
            criteria.andResourceLike("%" + student.getResource() + "%");
        }
        //学生姓名
        if(student.getStname()!=null&&!student.getStname().equals("")) {
            criteria.andStnameLike("%" + student.getStname() + "%");
        }
        //学生班级
        if(student.getCid()!=null&&!student.getCid().equals("")) {
            criteria.andCidEqualTo( student.getCid());
        }
        List<Integer> sidList=new ArrayList<>();
        List<Student> studentList=studentDao.selectByExample(studentExample);
        for (Student s:studentList) {
            sidList.add(s.getSid());
        }
        //成绩列表
        MarkExample markExample=new MarkExample();
        MarkExample.Criteria markExampleCriteria = markExample.createCriteria();
        markExampleCriteria.andSidIn(sidList);
        List<Mark> markList=markDao.selectByExample(markExample);
        for (Mark m3:markList) {
            if(m3.getSno()==1){
                m3.setFirstStage(m3.getScore6());
            }else if(m3.getSno()==2){
                m3.setSecondStage(m3.getScore6());
            }else if(m3.getSno()==3){
                m3.setThirdStage(m3.getScore6());
            }else if(m3.getSno()==4){
                m3.setProcessStage(m3.getScore6());
            }
        }
        System.out.println("marklist:"+markList);
        System.out.println("sidlist:"+sidList);

        List<Mark> markList2=new ArrayList<>();
        //列表去重
        boolean flag = false;
        for (Mark old : markList) {
            for (Mark news : markList2) {
                if (news.getSid() == old.getSid()) {
                    if(old.getSno()==1){
                        news.setFirstStage(old.getFirstStage());
                        news.setSno(old.getSno());
                    }
                    if(old.getSno()==2){
                        news.setSecondStage(old.getSecondStage());
                        news.setSno(old.getSno());
                    }
                    if(old.getSno()==3){
                        news.setThirdStage(old.getThirdStage());
                        news.setSno(old.getSno());
                    }
                    if(old.getSno()==4){
                        news.setProcessStage(old.getProcessStage());
                        news.setSno(old.getSno());
                    }
                    flag = true;
                    break;
                } else {
                    flag = false;
                }
            }
            if (!flag) {
                markList2.add(old);
            }
        }
        System.out.println(markList2);

        return markList2;
    }

    /**
     * 查询阶段考成绩
     * @param sid
     * @return
     */
    @Override
    public Mark selectMark1(Integer sid) {
        //成绩列表
        MarkExample markExample=new MarkExample();
        MarkExample.Criteria markExampleCriteria = markExample.createCriteria();
        markExampleCriteria.andSidEqualTo(sid);
        List<Mark> markList=markDao.selectByExample(markExample);
        for (Mark m3:markList) {
            if(m3.getSno()==1){
                m3.setFirstStage(m3.getScore6());
            }else if(m3.getSno()==2){
                m3.setSecondStage(m3.getScore6());
            }else if(m3.getSno()==3){
                m3.setThirdStage(m3.getScore6());
            }else if(m3.getSno()==4){
                m3.setProcessStage(m3.getScore6());
            }
        }
        System.out.println("marklist:"+markList);

        List<Mark> markList2=new ArrayList<>();
        //列表去重
        boolean flag = false;
        for (Mark old : markList) {
            for (Mark news : markList2) {
                if (news.getSid() == old.getSid()) {
                    if(old.getSno()==1){
                        news.setFirstStage(old.getFirstStage());
                        news.setSno(old.getSno());
                    }
                    if(old.getSno()==2){
                        news.setSecondStage(old.getSecondStage());
                        news.setSno(old.getSno());
                    }
                    if(old.getSno()==3){
                        news.setThirdStage(old.getThirdStage());
                        news.setSno(old.getSno());
                    }
                    if(old.getSno()==4){
                        news.setProcessStage(old.getProcessStage());
                        news.setSno(old.getSno());
                    }
                    flag = true;
                    break;
                } else {
                    flag = false;
                }
            }
            if (!flag) {
                markList2.add(old);
            }
        }
        System.out.println(markList2);
        return markList2.get(0);
    }

    /**
     * 查询学生综合成绩列表
     * @param mark
     * @param student
     * @return
     */
    @Override
    public List<Mark> getStudents1(Mark mark,Student student) {
        StudentExample studentExample=new StudentExample();
        StudentExample.Criteria criteria=studentExample.createCriteria();
        //来源信息
        if(student.getResource()!=null&&!student.getResource().equals("")) {
            criteria.andResourceLike("%" + student.getResource() + "%");
        }
        //学生姓名
        if(student.getStname()!=null&&!student.getStname().equals("")) {
            criteria.andStnameLike("%" + student.getStname() + "%");
        }
        //学生班级
        if(student.getCid()!=null&&!student.getCid().equals("")) {
            criteria.andCidEqualTo( student.getCid());
        }
        List<Integer> sidList=new ArrayList<>();
        List<Student> studentList=studentDao.selectByExample(studentExample);
        for (Student s:studentList) {
            sidList.add(s.getSid());
        }

        //成绩列表
        MarkExample markExample=new MarkExample();
        MarkExample.Criteria markExampleCriteria = markExample.createCriteria();
        //学生阶段
        if(mark.getSno()!=null&&!mark.getSno().equals("")) {
            markExampleCriteria.andSnoEqualTo( mark.getSno());
        }
        markExampleCriteria.andSidIn(sidList);
        List<Mark> markList=markDao.selectByExample(markExample);
        for (Mark m3:markList) {
            if(m3.getSno()==1){
                m3.setFirstStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }else if(m3.getSno()==2){
                m3.setSecondStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }else if(m3.getSno()==3){
                m3.setThirdStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }else if(m3.getSno()==4){
                m3.setProcessStage((m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5);
            }
            m3.setAllScore(m3.getScore6()*0.5+0.5*(m3.getScore1()+m3.getScore2()+m3.getScore3()+m3.getScore4()+m3.getScore5())/5+m3.getAttendance()+m3.getHomeworks());
        }

        System.out.println("marklist:"+markList);
        System.out.println("sidlist:"+sidList);

        return markList;
    }



}








